<?php
// include constants
include "../includes/vars.php";
include "../includes/containers.php";
include "newvars.php";
$pathToRoot = "../"; 		// or "";

?>

<?php
include "header.php";
?>


<?php
// content php code goes here

$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die('Cannot connect to database '.$dbhost.'+'. $dbuser.'+'. $dbpass.'+'.mysql_error());

mysql_select_db($dbname)
  or Die("Database not found.");

$hLine = '<HR color=#BCCBFC>';
function getReadBy($readNuumber){
	return '<P>Read by : ' . $readNuumber . ' <BR></P>';
}

function getDwonloadBookLink($link){
	return '<A HREF="index.php?act=download&BID=' . $link . '"><img src="images/downloadThisBook.gif" BORDER=0></A>';
}

function getExclusiveDetail($exc_detail_link, $exc_image, $exc_name, $exc_author_link, $exc_author_name, $exc_read_by_count, $exc_download_link) {
	//walid.elswaify
	$result = '<div class="book_details">
								<div class="home_header_image float_left height100">
									<a href="' . $exc_detail_link . '">
									<img src="'. $exc_image . '" alt="'. $exc_name . '" width="52" height="76" /></a>							</div>
								<div class="content_header_details">
								<h3>' . $exc_name . '</h3>
								By: <a href="' . $exc_author_link . '">
								' . $exc_author_name . '
								</a>
								
								<p class="blue">

									<strong>Read by:</strong> ' . $exc_read_by_count  . '<br />
								</p>
								<p>
									<a href="' . $exc_download_link .'" class="link_download_home">Download this book</a>							
									</p>
								</div>
							</div>
							<div class="clear"></div>';
							
							
	return $result;
}

function getPomDetail($book_detail_link, $book_image, $book_name, $book_author_link, $book_author_name, $book_read_by_count, $book_download_link) {
	$result = '<div class="book_details">
								<div class="home_header_image float_left">
								<a href="' . $book_detail_link . '">
									<img src="' . $book_image . '" alt="' . $book_name . '" width="84" height="126" /></a>							</div>
								<div class="content_header_details">
								<h3>' . $book_name . '</h3>
								By: <a href="' . $book_author_link . '">
								' . $book_author_name . '
								</a>
								
								<p class="blue">

									<strong>Read by:</strong> ' . $book_read_by_count . '<br />
															</p>
								<p>
									<a href="' . $book_download_link . '" class="link_download_home">Download this book</a>							</p>

								</div>
							</div>';
							
							return $result;
}

//walid.elswaify
/*** Code goes here **/ 




    // exclusive books
    $books_exc = "";
    $sql = "SELECT * FROM books WHERE B_TYPE_EXC = 1 ORDER BY `B_ID` DESC LIMIT 0," . $bktoshownum;

    $result = mysql_query($sql);
    $numrecords = mysql_num_rows($result);

    while ($row = mysql_fetch_row($result)){
    
        $authsql ="SELECT BA_AUTH_ID FROM bk_auth WHERE BA_BOOK_ID = " . $row[0];
        if ($authresult = mysql_query($authsql)){
            $authrow = mysql_fetch_row($authresult);
            
            $authsql2 ="SELECT * FROM authors WHERE A_ID = " . $authrow[0];
            if($authresult2 = mysql_query($authsql2)){
            
                $authrow2 = mysql_fetch_row($authresult2);
            
            }
            

        }

		
		$exc_detail_link = 'view.php?bkID=' . $row[0] ;
		$exc_name = $row[1];
		$exc_image = $websitename . $row[3];
		$exc_author_link = 'view.php?authID=' . $authrow2[0];
		$exc_author_name = $authrow2[1] . " " . $authrow2[2];
		$exc_read_by_count = $row[4];
		
		$exc_download_link = '#';
		if (trim($row[2]) != "" && trim($row[2]) != "NULL"){        
		$exc_download_link = 'index.php?act=download&BID=' . $row[0];
		}
		
		$exc_list = $exc_list . getExclusiveDetail($exc_detail_link, $exc_image, $exc_name, $exc_author_link, $exc_author_name, $exc_read_by_count, $exc_download_link);
		
        

    }
    

        $sql = "SELECT B_ID FROM books WHERE B_TYPE_EXC = 1 ";
        if ($result = mysql_query($sql)){
            $numrecords = mysql_num_rows($result);

        }
        else{
            $numrecords = 5;
        }



        $numpages = round($numrecords/$bktoshownum);
        

        if (($numpages-($numrecords/$bktoshownum))<0){
            $numpages += 1;
        }
        
		$exc_navigation = '<div class="content_pagination">Page 1 of ' . $numpages;

        if ($numpages > 1){
            $exc_navigation = $exc_navigation . ' <a href="index.php?act=list&cat=exc&page=2">Next &raquo;</a>';
        }
		
		 $exc_navigation = $exc_navigation . '</div>';
		
							
							
							






    // pick of the month
    $books_potm = "";
    $sql = "SELECT * FROM books WHERE B_TYPE_POTM = 1  ORDER BY `B_ID` DESC LIMIT 0," . $bktoshownum;

    $result = mysql_query($sql);
    
    while ($row = mysql_fetch_row($result)){

        $authsql ="SELECT BA_AUTH_ID FROM bk_auth WHERE BA_BOOK_ID = " . $row[0];
        if ($authresult = mysql_query($authsql)){
            $authrow = mysql_fetch_row($authresult);

            $authsql2 ="SELECT * FROM authors WHERE A_ID = " . $authrow[0];
            if($authresult2 = mysql_query($authsql2)){

                $authrow2 = mysql_fetch_row($authresult2);

            }


        }


		//walid.elswaify
		$book_detail_link = 'view.php?bkID=' . $row[0] ;
		$book_name = $row[1];
		$book_image = $websitename . $row[3];
		$book_author_link = 'view.php?authID=' . $authrow2[0];
		$book_author_name = $authrow2[1] . " " . $authrow2[2];
		$book_read_by_count = $row[4];
		$book_download_link = '#';
		if (trim($row[2]) != "" && trim($row[2]) != "NULL"){
            $book_download_link = 'index.php?act=download&BID=' . $row[0];
        }
		
		$pom_list = $pom_list . getPomDetail($book_detail_link, $book_image, $book_name, $book_author_link, $book_author_name, $book_read_by_count, $book_download_link);

    }
    

 
    
    // New books
    $books_new = "";
    $sql = "SELECT * FROM books WHERE B_TYPE_NEW = 1 ORDER BY `B_ID` DESC LIMIT 0," . $bktoshownum;

    $result = mysql_query($sql);


    while ($row = mysql_fetch_row($result)){

        $authsql ="SELECT BA_AUTH_ID FROM bk_auth WHERE BA_BOOK_ID = " . $row[0];
        if ($authresult = mysql_query($authsql)){
            $authrow = mysql_fetch_row($authresult);

            $authsql2 ="SELECT * FROM authors WHERE A_ID = " . $authrow[0];
            if($authresult2 = mysql_query($authsql2)){

                $authrow2 = mysql_fetch_row($authresult2);

            }


        }


		
	    $new_detail_link = 'view.php?bkID=' . $row[0] ;
		$new_name = $row[1];
		$new_image = $websitename . $row[3];
		$new_author_link = 'view.php?authID=' . $authrow2[0];
		$new_author_name = $authrow2[1] . " " . $authrow2[2];
		$new_read_by_count = $row[4];
		$new_download_link = '#';
		if (trim($row[2]) != ""  && trim($row[2]) != "NULL"){  
			$new_download_link = 'index.php?act=download&BID=' . $row[0];
		}
		
		$new_list = $new_list . getExclusiveDetail($new_detail_link, $new_image, $new_name, $new_author_link, $new_author_name, $new_read_by_count, $new_download_link);


    }

        $sql = "SELECT B_ID FROM books WHERE B_TYPE_NEW = 1";
        if ($result = mysql_query($sql)){
            $numrecords = mysql_num_rows($result);
            
        }
        else{
            $numrecords = 5;
        }

        $numpages = round($numrecords/$bktoshownum);
        
        if (($numpages-($numrecords/$bktoshownum))<0){

            $numpages += 1;
        }
 
			$new_navigation = '<div class="content_pagination">Page 1 of ' . $numpages;
		
        if ($numpages > 1){
            $new_navigation = $new_navigation . ' <a href="index.php?act=list&cat=exc&page=2">Next &raquo;</a>';
        }
		
		 $new_navigation = $new_navigation . '</div>';
        
        
        		
?>
<!-- content html code goes here-->
              <td rowspan="2" class="content_center" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5">
                <tr>
                  <td width="50%">
				  <div class="home_box">
				  		<div class="home_box_header">
							<div class="calendar_img">
								<span> Pick of the month</span>
							</div>

						</div>
						<div class="home_box_cont">
							<?php print $pom_list;?>
							</div>
						</div>
				  </div>
				  </td>
                  <td width="50%" rowspan="2" valign="top">
				  <div class="home_box">
				  		<div class="home_box_header">
							<div class="new_img">

								<span> What's New</span>
							</div>
						</div>
						<div class="home_box_cont">
							
							<?php print $new_list?>
							<?php print $new_navigation?>
							
						</div>
				  </div>
				  </td>
                </tr>

                <tr>
                  <td width="50%">
				  <div class="home_box">
				  		<div class="home_box_header">
							<div class="exclusive_img">
								<span> IslamBasics Exculsive</span>
							</div>
						</div>

						<div class="home_box_cont">
						
							
							
								
								<?php print $exc_list ?>
								<?php print $exc_navigation ?>
								
							
				  </div>
				  </td>
                </tr>

              </table></td>

<!-- end of content html code-->



<?php
include "footer.php";
?>
